package com.kye.express.business.president_online;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemPresidentOnlineListBinding;
import com.kye.express.utils.DateTimeUtils;
import com.kye.express.utils.StringUtils;
import com.kye.model.bean.president_online.PresidentOnlineListBean;

import io.reactivex.functions.Consumer;

class PresidentOnlineListItemViewBinder extends me.drakeet.multitype.ItemViewBinder<PresidentOnlineListBean.DatasBean, PresidentOnlineListItemViewBinder.ViewHolder> {

    private Context mContext;
    private PresidentOnlineListener mListener;

    public PresidentOnlineListItemViewBinder(Context context,PresidentOnlineListener listener) {
        mContext = context;
        mListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_president_online_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull PresidentOnlineListBean.DatasBean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private PresidentOnlineListBean.DatasBean data;
        ItemPresidentOnlineListBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(PresidentOnlineListBean.DatasBean item) {
            data = item;
            if (data != null) {
                dataBinding.setDatas(data);

                String time = data.getCreateTime().replaceAll("T", " ");
                try {
                    String time1 = DateTimeUtils.getStringDateByMounthDay(DateTimeUtils.getOderTime(time));
                    dataBinding.tvPresidentHotlineCreatedt.setText(time1);
                } catch (Exception e) {
                    e.printStackTrace();
                }


                if (StringUtils.isNullOrEmpty(data.getAudioSrc())) {
                    //            文本
                    dataBinding.tvMessage.setText(data.getMessage());
                    dataBinding.tvMessage.setTextColor(mContext.getResources().getColor(R.color.black_888));
                } else {
                    //            语音
                    dataBinding.tvMessage.setText(R.string.president_online_voice_message);
                    dataBinding.tvMessage.setTextColor(mContext.getResources().getColor(R.color.president_online_voice_text_color));
                }

                if (StringUtils.isNullOrEmpty(data.getReplyAudioSrc())) {
                    //            回复信息为文本信息
                    dataBinding.tvReply.setText(data.getReply());
                    dataBinding.tvReply.setTextColor(mContext.getResources().getColor(R.color.black_888));
                } else {
                    //            回复信息为语音信息
                    dataBinding.tvReply.setText(R.string.president_online_voice_message);
                    dataBinding.tvReply.setTextColor(mContext.getResources().getColor(R.color.black_888));
                }

                String status = data.getHandleStatus();//信息状态
                if ("总裁转发".equals(status)) {
                    dataBinding.llEditLayout.setVisibility(View.GONE);
                    dataBinding.tvPresidentHotlineCreatedt.setTextColor(mContext.getResources().getColor(R.color.president_online_red));
                    dataBinding.tvResponseTime.setTextColor(mContext.getResources().getColor(R.color.president_online_red));
                } else if ("处理中".equals(status)) {
                    dataBinding.llEditLayout.setVisibility(View.GONE);
                } else if ("未处理".equals(status)) {
                    dataBinding.llEditLayout.setVisibility(View.VISIBLE);
                    dataBinding.tvPresidentHotlineCreatedt.setTextColor(mContext.getResources().getColor(R.color.president_online_red));
                    dataBinding.tvResponseTime.setTextColor(mContext.getResources().getColor(R.color.president_online_red));
                } else if ("已完结".equals(status)) {
                    dataBinding.llEditLayout.setVisibility(View.GONE);
                }

                if ("未处理".equals(status)){
                    dataBinding.tvInfoType.setTextColor(mContext.getResources().getColor(R.color.president_online_red));//如果是未处理，字体改为红色
                }else{
                    dataBinding.tvInfoType.setTextColor(mContext.getResources().getColor(R.color.black_999));
                }
                dataBinding.btnEdit.setVisibility(data.isIsEdit() ? View.VISIBLE : View.GONE);
                dataBinding.tvInfoType.setText(status);

                RxView.clicks(dataBinding.btnEdit)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                if (mListener != null){
                                    mListener.onEdit(data);
                                }
                            }
                        });

                RxView.clicks(dataBinding.getRoot())
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                if (mListener != null){
                                    mListener.onGoToDetail(data);
                                }
                            }
                        });
            }
        }
    }
}
